package com.cn.yz.springboot.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import com.cn.yz.springboot.jersey.Page;
import com.cn.yz.springboot.jersey.PagedData;
import com.cn.yz.springboot.mapper.SysUserMapper;
import com.cn.yz.springboot.model.SysUser;
import com.cn.yz.springboot.service.SysUserService;
import com.github.pagehelper.PageHelper;

@Service
public class SysUserServiceImpl implements SysUserService {
	
	private static final Logger logger = LoggerFactory.getLogger(SysUserServiceImpl.class);
	
	@Autowired
	private RedisTemplate redisTemplate;
	
	@Autowired
	SysUserMapper sysUserMapper;
	
	@Resource(name="redisTemplate")  
    ValueOperations<Integer,SysUser> valOps;  

	@Override
	public SysUser selectByPrimaryKey(Integer id) {
		SysUser sysUser = null;
//		sysUser = valOps.get(id);
//		logger.info("valOps.get("+id+"+):"+sysUser);
//		if(sysUser!=null){
//			return sysUser;
//		}
//		logger.info("redis中没有找到数据");
		sysUser = sysUserMapper.selectByPrimaryKey(id);
//		logger.info("valOps.set("+id+"+)"+sysUser);
//		valOps.set(id, sysUser);
		return sysUser;
	}

	@Override
	public boolean addSysUser(SysUser sysUser) {
		sysUser.setCreateTime(new Date());
		return sysUserMapper.insertSelective(sysUser)>0;
	}

	@Override
	public PagedData<SysUser> querySysUser(@Param("condition")Map<String, Object> condition) {
		Page page = null;
		if(condition.get("pageIndex")!=null && condition.get("pageSize")!=null){
			int pageIndex = (Integer)condition.get("pageIndex");
			int pageSize = (Integer)condition.get("pageSize");
			if(pageIndex > -1 && pageSize > -1)
			page = new Page(pageIndex,pageSize);
		}
		
		List<SysUser> sysUsers = sysUserMapper.findPageByCondition(condition,page);
		return new PagedData<SysUser>(page,sysUsers);
	}

}
